package com.qiandw.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * @author coxx
 */
@Slf4j
public final class Base64Utils {

    public static String encodeBase64(String str){
        String asB64 = null;
        try {
            asB64 = Base64.getEncoder().encodeToString(str.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("Base64 encode 错误");
        }
        return asB64;
    }

    public static String decodeBase64(String encodeStr) {
        byte[] asBytes = null;
        try {
            asBytes = Base64.getDecoder().decode(encodeStr.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            log.error("Base64 decode 错误");
        }
        return new String(asBytes);
    }

    public static Boolean verifty(String str, String encodeStr){
        return Base64.getEncoder().encodeToString(str.getBytes()).equals(encodeStr);
    }

    public static void main(String[] args) {
        String str = "江文杰";
        String encodeStr = encodeBase64(str);
        System.out.println(encodeStr);
        boolean isVerity = verifty(str, encodeStr);
        System.out.println(isVerity);
        System.exit(0);
    }
}
